<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\VoteTags;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Str;

class VoteTagsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VoteTags(),function(Grid $grid) {

            // 禁止
            $grid->toolsWithOutline(false);

            $grid->disablePagination();

            $grid->disableRowSelector();

            $grid->disableBatchDelete();

            $grid->quickCreate(function(Grid\Tools\QuickCreate $create) {
                $create->text('sort_id','排序');
                $create->text('name','名称');
            });

            $grid->setActionClass(Grid\Displayers\Actions::class);

            $grid->column('sort_id')->editable(true)->sortable();

            $grid->column('name')->editable(true);

            $grid->column('is_using')->switch('',true);

            $grid->column('created_at')->display(function($value) {
                return date('Y-m-d h:i',strtotime($value));
            })->sortable();

            $grid->column('updated_at')->display(function($value) {
                return date('Y-m-d h:i',strtotime($value));
            })->sortable();

            $grid->filter(function(Grid\Filter $filter) {
                $filter->equal('name');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id,new VoteTags(),function(Show $show) {
            $show->field('name');
            $show->field('sort_id');
            $show->field('description')->unescape();
            $show->field('is_using')->using([1 => '启用',0 => '禁用']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VoteTags(),function(Form $form) {

            $form->disableResetButton();

            $form->disableEditingCheck();

            $form->disableViewCheck();

            $form->hidden('tags_key');

            $form->text('name')->required('min:1')->autofocus();

            $form->number('sort_id');

            $form->editor('description')->height(800);

            $form->radio('is_using')->options([1 => '开启',0 => '禁用'])->default(1)->required('min:1');

            // 保存时
            $form->saving(function(Form $form) {
                if(empty($this->t_key)) {
                    $form->t_key = Str::uuid();
                }
            });

        });
    }
}
